package com.example.park2212.devices

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityDevicesBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/devices/DevicesActivity")
class DevicesActivity : BaseActivity<ActivityDevicesBinding,DevicesVewModel>() {
    lateinit var devicesAdapter: DevicesAdapter
    override fun initView() {
        binding.devicesHead.headerTitle.setText("设备管理")
        binding.devicesHead.headerBack.setOnClickListener {
            finish()
        }

        devicesAdapter = DevicesAdapter()
        binding.rvDevices.adapter = devicesAdapter
        binding.rvDevices.layoutManager = LinearLayoutManager(this)

        binding.devicesHead.headerAdd.setOnClickListener {
            ARouter.getInstance().build( "/app/devices/DevicesAddActivity").navigation()
        }

        binding.devicesSv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query != null){
                    viewModel.devicesSearch(query)
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
               return true
            }
        })

    }

    override fun initData() {
        viewModel.getDevicesSuccess.observe(this){
            devicesAdapter.data.clear()
            devicesAdapter.data.addAll(it)
            devicesAdapter.notifyDataSetChanged()
        }
        viewModel.devicesSearchSuccess.observe(this){
            devicesAdapter.data.clear()
            devicesAdapter.data.addAll(it)
            devicesAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getDevices()
    }

}